package com.kedacom.demo.datasource;

import java.util.Random;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BusinessService {

	@Resource
	private JdbcTemplate jdbcTemplate;

	public void initData() {
		System.out.println("start");
		for (int i = 1; i <= 10000; i++) { // 初始化插入10000条数据
			jdbcTemplate.execute("insert into users(name) values('test_name_" + i + "')");
			if (i % 1000 == 0) {
				System.out.println(i);
			}
		}
		System.out.println("end");
	}

	public void testQueryBusiness() {
		int n = new Random().nextInt(10000);
		jdbcTemplate.queryForList("select name from users limit ?,100", String.class, n);
		try {
			Thread.sleep(n / 2 + 1); // 休眠1～6秒
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
